package com.zhuiyun.project.api.video.userVideo.service.impl;

import com.zhuiyun.project.api.video.publishVideo.entity.Video;
import com.zhuiyun.project.api.video.publishVideo.model.PublishVideoModel;
import com.zhuiyun.project.api.video.userVideo.mapper.UserVideoMapper;
import com.zhuiyun.project.api.video.userVideo.service.UserVideoService;
import com.zhuiyun.project.common.errorCode.CommonException;
import com.zhuiyun.project.common.errorCode.EmErrorCode;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.utils.pageUtil.MybatisPageHelper;
import com.zhuiyun.project.utils.pageUtil.PageRequest;
import com.zhuiyun.project.utils.pageUtil.PageResult;
import com.zhuiyun.project.utils.validator.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Transient;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @ClassName userVideoServiceImpl
 * @Description TODO
 * @Author Administrator
 * @Date 2024/2/26 9:12
 **/
@Service
@Transactional
public class UserVideoServiceImpl implements UserVideoService {

    @Autowired
    private UserVideoMapper userVideoMapper;

    /**
    * @Author qsh
    * @Description 查询分发视频
    * @Date 2024/2/26 9:51
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param publishVideoModel
* @param pageRequest
    */
    @Override
    public CommonResult getUserVideoF(PublishVideoModel publishVideoModel, PageRequest pageRequest) {
        // 设置分页
        MybatisPageHelper.startPage(pageRequest);
        PageResult pageResult = MybatisPageHelper.getPageResult(userVideoMapper.getUserVideoF(publishVideoModel));
        return CommonResult.ok(pageResult);
    }

    /**
    * @Author qsh
    * @Description 查询推荐视频
    * @Date 2024/2/26 9:51
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param publishVideoModel
* @param pageRequest
    */
    @Override
    public CommonResult getUserVideoT(PublishVideoModel publishVideoModel, PageRequest pageRequest) {
        // 设置分页
        MybatisPageHelper.startPage(pageRequest);
        PageResult pageResult = MybatisPageHelper.getPageResult(userVideoMapper.getUserVideoT(publishVideoModel));
        return CommonResult.ok(pageResult);
    }

    /**
    * @Author qsh
    * @Description 查询低质量视频
    * @Date 2024/2/26 9:51
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param publishVideoModel
* @param pageRequest
    */
    @Override
    public CommonResult getUserVideoD(PublishVideoModel publishVideoModel, PageRequest pageRequest) {
        // 设置分页
        MybatisPageHelper.startPage(pageRequest);
        PageResult pageResult = MybatisPageHelper.getPageResult(userVideoMapper.getUserVideoD(publishVideoModel));
        return CommonResult.ok(pageResult);
    }

    /**
    * @Author qsh
    * @Description 查询运营下架视频
    * @Date 2024/2/26 9:52
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param publishVideoModel
* @param pageRequest
    */
    @Override
    public CommonResult getUserVideoX(PublishVideoModel publishVideoModel, PageRequest pageRequest) {
        // 设置分页
        MybatisPageHelper.startPage(pageRequest);
        PageResult pageResult = MybatisPageHelper.getPageResult(userVideoMapper.getUserVideoX(publishVideoModel));
        return CommonResult.ok(pageResult);
    }

    /**
    * @Author qsh
    * @Description 查询用户自删视频
    * @Date 2024/2/26 9:53
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param publishVideoModel
* @param pageRequest
    */
    @Override
    public CommonResult getUserVideoS(PublishVideoModel publishVideoModel, PageRequest pageRequest) {
        // 设置分页
        MybatisPageHelper.startPage(pageRequest);
        PageResult pageResult = MybatisPageHelper.getPageResult(userVideoMapper.getUserVideoS(publishVideoModel));
        return CommonResult.ok(pageResult);
    }

    /**
    * @Author qsh
    * @Description 绑定低质量视频
    * @Date 2024/2/26 14:00
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param videoIds
    */
    @Override
    public CommonResult DeleteVideoById(List<Integer> videoIds) throws CommonException {
        // 验证参数的规范性
        if (ValidatorUtil.ValidationObject(videoIds)) {
            // 创建 Example 对象
            for (Integer vId : videoIds) {
                Example example = new Example(Video.class);
                example.createCriteria().andEqualTo("vId", vId); // 设置要删除的记录的主键ID条件
                List<Video> publishVideos = userVideoMapper.selectByExample(example);
                for (Video publishVideo : publishVideos) {
                    publishVideo.setBindVideo(2);
                    userVideoMapper.updateByExampleSelective(publishVideo, example);
                }
            }
            return CommonResult.ok(EmErrorCode.DELETE_ERROR);
        } else {
            return CommonResult.ok(EmErrorCode.DELETE_ERROR);
        }
    }
}
